<?php
/**
* 게시판 설정 로직 처리
* /ezadm/logic/board/ezbbs_config.php
* ezdong <ezdong@nextez.co.kr> & hamhaja <hamhaja@nextez.co.kr>
* 2010-03-03
*/
// 경로 / 인증 체크
require_once dirname(__FILE__) . '/../../inc.auth.php';

// class instance
$App->class_require('EzbbsManager');
$App->class_require('Paging');
$App->class_require('Table.Ezbbscnf');
$App->class_require('Table.EzbbscnfArtcat');
$App->class_require('Table.EzbbscnfAdd');
$App->class_require('Table.EzbbsgrpEzbbscnf');
$App->class_require('Table.Ezbbs');
$EzbbsManager	= new EzbbsManager();
$Paging			= new Paging();
$Ezbbscnf		= new Ezbbscnf(&$Db);
$EzbbscnfArtcat	= new EzbbscnfArtcat(&$Db);
$EzbbscnfAdd	= new EzbbscnfAdd(&$Db);
$EzbbsgrpEzbbscnf = new EzbbsgrpEzbbscnf(&$Db);
$Ezbbs			= new Ezbbs(&$Db);

if ($g_subjob == 'list' || $g_subjob == 'search')
{
	// set init
	$page = $App->nvl('page', 1);
	$column = $App->nvl('column');
	$keyword = $App->nvl('keyword');

	// set paging
	$wheres = array();
	if ($column && !array_key_exists($column, $Ezbbscnf->_table_fields)) $column = '';
	if (!$column) $keyword = '';
	if ($keyword) $wheres[] = "{$column} LIKE '%{$keyword}%'";
	$Ezbbscnf->paging(&$Paging, $wheres, $page, 15, 10);
	$ezbbs_list = $Ezbbscnf->get_list('ebcf_id, ebcf_pname, ebcf_answer_flag, ebcf_reply_flag, ebcf_file_num, ebcf_artcat_flag, ebcf_use_flag, ebcf_ctime', $wheres, 'ebcf_id');
	$Paging->set_total_num($Ezbbscnf->count());

	// count article
	foreach ($ezbbs_list as $k => $v)
		$ezbbs_list[$k]['article_num'] = $Ezbbs->count_in_board($v['ebcf_id']);

	if ($keyword && $column == 'ebcf_pname')
	{
		foreach ($ezbbs_list as $k => $v)
			$ezbbs_list[$k][$column] = $App->keyword($keyword, $v[$column]);
	}
}
else if ($g_subjob == 'insert')
{
	if (!$App->check_method(POST)) $Error->fail(NO_PERMIT_METHOD);

	if (!$Ezbbscnf->check(array($_POST['ebcf_id'])))
	{
		$_POST['ebcf_skin2'] = "default.css";

		//게시판 생성시 디폴트 제한태그 디폴트 값을 정한다.(2010.03.04 ksb)
		$_POST['ebcf_tag_list'] = "script,object,applet,form,embed,iframe,frame,base,body,frameset,html,img,layer,ilayer,meta,p,style,xxx src,a href";

		//2011-12-06 추가 by shin
		$_POST['ebcf_file_dir'] = $App->_dir_board . '/data/' . $_POST['ebcf_id'];
		$_POST['ebcf_file_url'] = $App->_url_board . '/data/' . $_POST['ebcf_id'];

		if ($Ezbbscnf->insert($_POST, array('ebcf_cid', 'ebcf_ctime')))
		{
			// create board data directory
			$dir_path = $App->_dir_board . '/data/' . $_POST['ebcf_id'];

			if (!is_dir($dir_path))
			{
				if (!@mkdir($dir_path, 0777)) $Error->fail(DIR_CREATE_FAIL);
			}
			@chmod($dir_path, 0777);
			$Script->alert("생성처리하였습니다.");
			$App->reload();
		}
		else $Error->fail(INSERT_FAIL);
	}
	else $Error->fail(IS_ID);
}
else if ($g_subjob == 'update_form')
{
	// set global params
	if (!$_GET['ebcf_id'] && $_GET['id1'])
	{
		$_GET['g_params'] = "ebcf_id=" . $_GET['id1'];
		$_GET['ebcf_id'] = $_GET['id1'];
	}

	if ($config_record = $Ezbbscnf->get_a_record('', array($_GET['ebcf_id'])))
	{
		$Ezbbscnf->htmlchars($config_record);

		// set datas
		$langs = $Ezbbscnf->_langs;

		// get logic / skin list
		$logic_list = & $Ezbbscnf->logic_list();
		$skin_list = & $Ezbbscnf->skin_list();
		$css_list = & $Ezbbscnf->css_list();

		// split fields
		$config_record['ebcf_list_list']	= split(',', $config_record['ebcf_list_list']);
		$config_record['ebcf_input_list']	= split(',', $config_record['ebcf_input_list']);
		$config_record['ebcf_view_list']	= split(',', $config_record['ebcf_view_list']);
		$config_record['ebcf_search_list']	= split(',', $config_record['ebcf_search_list']);

		// set list fields
		$list_fields = array('no' => '번호', 'name' => '작성자', 'file' => '첨부파일', 'cdate' => '등록일', 'cnt' => '조회수');
		$list_els = $Html->check_button_els('ebcf_list_list[]', $list_fields, $config_record['ebcf_list_list']);

		// set input fields
		$input_fields = array('name' => '작성자', 'eml' => '이메일', 'url' => '홈페이지');
		$input_els = $Html->check_button_els('ebcf_input_list[]', $input_fields, $config_record['ebcf_input_list']);

		// set view fields
		$detail_fields = array('no' => '번호', 'name' => '작성자', 'mid' => '회원아이디', 'cdate' => '등록일', 'udate' => '수정일', 'cnt' => '조회수', 'eml' => '이메일', 'url' => '홈페이지', 'cip' => '등록IP', 'uip' => '수정IP');
		$detail_els = $Html->check_button_els('ebcf_view_list[]', $detail_fields, $config_record['ebcf_view_list']);
		$detail_els = array_chunk($detail_els, 5);

		// set search fields
		$search_fields = array('name' => '작성자', 'subject' => '제목', 'txt' => '내용');
		$search_els = $Html->check_button_els('ebcf_search_list[]', $search_fields, $config_record['ebcf_search_list']);

		// set order fields
		$order_fields = array('D' => '관련글', 'P' => '일련번호', 'S' => '제목', 'C' => '조회수');
	}
	else $Error->notice(NOT_FIND_DATA);
}
else if ($g_subjob == 'update')
{
	if (!$App->check_method(POST)) $Error->fail(NO_PERMIT_METHOD);

	// set option
	if (!isset($_POST['ebcf_gallery_flag']))	$_POST['ebcf_gallery_flag'] = 0;
	if (!isset($_POST['ebcf_order_flag']))		$_POST['ebcf_order_flag'] = 1;

	// set list fileds
	if (!isset($_POST['ebcf_list_list'])) $_POST['ebcf_list_list'] = '';
	else $_POST['ebcf_list_list'] = implode(',', $_POST['ebcf_list_list']);

	// set input fields
	if (!isset($_POST['ebcf_input_list'])) $_POST['ebcf_input_list'] = '';
	else $_POST['ebcf_input_list'] = implode(',', $_POST['ebcf_input_list']);

	// set view fields
	if (!isset($_POST['ebcf_view_list'])) $_POST['ebcf_view_list'] = '';
	else $_POST['ebcf_view_list'] = implode(',', $_POST['ebcf_view_list']);

	// set search fileds
	if (!isset($_POST['ebcf_search_list'])) $_POST['ebcf_search_list'] = '';
	else $_POST['ebcf_search_list'] = implode(',', $_POST['ebcf_search_list']);

	if ($Ezbbscnf->update($_POST, array($_POST['ebcf_id']), array('ebcf_uid', 'ebcf_utime'))) $App->reload(true);
	else $Error->fail(UPDATE_FAIL);
}
else if ($g_subjob == 'date_format')
{

}
else if ($g_subjob == 'delete')
{
	if (!$Ezbbs->count_in_board($_GET['id1']))
	{
		// 관계레코드 삭제처리
		$EzbbscnfArtcat->delete_in_board($_GET['id1']);
		$EzbbscnfAdd->delete_in_board($_GET['id1']);
		$EzbbsgrpEzbbscnf->delete_as_board($_GET['id1']);

		if ($Ezbbscnf->delete(array($_GET['id1'])))
		{
			// delete board data directory
			@rmdir($App->_dir_board . '/data/' . $_GET['id1']);
			$App->reload();
		}
		else $Error->fail(DELETE_FAIL);
	}
	else $Error->fail(DELETE_FAIL, '게시물이 존재하는 게시판은 삭제가 불가합니다.');
}
else if ($g_subjob == 'copy_form')
{
	$config_record = & $Ezbbscnf->get_a_record('ebcf_id, ebcf_pname', array($_GET['id1']));
	if (!$config_record) $Error->notice(NOT_FIND_DATA);
}
else if ($g_subjob == 'copy')
{
	if (!$App->check_method(POST)) $Error->fail(NO_PERMIT_METHOD);

	// class instance
	$App->class_require('Table.EzbbscnfArtcat');
	$App->class_require('Table.EzbbsgrpEzbbscnf');
	$EzbbscnfArtcat	= new EzbbscnfArtcat(&$Db);
	$GroupConfig	= new EzbbsgrpEzbbscnf(&$Db);

	if ($Ezbbscnf->check(array($_POST['ebcf_id']))) $Error->fail(IS_ID);

	// 대상 설정정보
	$cols = $Ezbbscnf->get_a_record('', array($_POST['src_id']));
	$cols['ebcf_id']	= $_POST['ebcf_id'];
	$cols['ebcf_pname']	= $_POST['ebcf_pname'];

//2011-12-06 추가 by shin
	$cols['ebcf_file_dir'] = $App->_dir_board . '/data/' . $cols['ebcf_id'];
	$cols['ebcf_file_url'] = $App->_url_board . '/data/' . $cols['ebcf_id'];

	unset($cols['ebcf_uid']);
	unset($cols['ebcf_utime']);

	if ($Ezbbscnf->insert($cols, array('ebcf_cid', 'ebcf_ctime')))
	{
		$dir_path = $App->_dir_board . '/data/' . $_POST['ebcf_id'];
		if (!is_dir($dir_path))
		{
			if (!mkdir($dir_path, 0777)) $Error->fail(DIR_CREATE_FAIL);
		}
		@chmod($dir_path, 0777);
		// 복사시 설정된 옵션항목을 복사처리합니다.
		if (isset($_POST['options']))
		{
			// copy add
			if (in_array('add', $_POST['options'])) $EzbbscnfAdd->copy_in_board($_POST['src_id'], $_POST['ebcf_id']);
			// copy category
			if (in_array('category', $_POST['options'])) $EzbbscnfArtcat->copy_in_board($_POST['src_id'], $_POST['ebcf_id']);
			// copy group
			if (in_array('group', $_POST['options'])) $GroupConfig->copy_in_board($_POST['src_id'], $_POST['ebcf_id']);
		}

		$Script->alert("복사처리하였습니다.", false);
		$Script->reload("parent.opener", false);
		$Script->close("parent");
	}
	else $Error->fail(INSERT_FAIL);
}
?>